<?php
/**
 * Author: Traveler
 * Time  : 2021/8/31 17:00
 */

namespace app\model;


use sunshine\basic\BaseModel;
use think\model\relation\BelongsTo;

class StaffModel extends BaseModel
{
    /**
     * 模型名称
     *
     * @var string
     */
    protected $name = 'staff';

    /**
     * 数据输出隐藏的属性
     *
     * @var string[]
     */
    protected $hidden = ['password', 'delete_time'];

    /**
     * 搜索器：id
     *
     * @param $query
     * @param $value
     * @param $data
     */
    public function searchIdAttr($query, $value, $data)
    {
        $query->where('id', $value);
    }

    /**
     * 搜索器：name
     *
     * @param $query
     * @param $value
     * @param $data
     */
    public function searchNameAttr($query, $value, $data)
    {
        $query->whereLike('name', '%' . $value . '%');
    }

    /**
     * 搜索器：cellphone
     *
     * @param $query
     * @param $value
     * @param $data
     */
    public function searchCellphoneAttr($query, $value, $data)
    {
        $query->whereLike('cellphone', '%' . $value . '%');
    }

    /**
     * 搜索器：email
     *
     * @param $query
     * @param $value
     * @param $data
     */
    public function searchEmailAttr($query, $value, $data)
    {
        $query->whereLike('email', '%' . $value . '%');
    }

    /**
     * 搜索器：gender
     *
     * @param $query
     * @param $value
     * @param $data
     */
    public function searchGenderAttr($query, $value, $data)
    {
        $query->where('gender', $value);
    }

    /**
     * 搜索器：department
     *
     * @param $query
     * @param $value
     * @param $data
     */
    public function searchDepartmentAttr($query, $value, $data)
    {
        $query->where('department_id', $value);
    }

    /**
     * 搜索器：role
     *
     * @param $query
     * @param $value
     * @param $data
     */
    public function searchRoleAttr($query, $value, $data)
    {
        $query->where('role_id', $value);
    }

    /**
     * 搜索器：status
     *
     * @param $query
     * @param $value
     * @param $data
     */
    public function searchStatusAttr($query, $value, $data)
    {
        $query->where('status', $value);
    }

    /**
     * 模型关联：部门
     *
     * @return BelongsTo
     */
    public function department(): BelongsTo
    {
        return $this->BelongsTo(DepartmentModel::class, 'department_id');
    }

    /**
     * 模型关联：角色
     *
     * @return BelongsTo
     */
    public function role(): BelongsTo
    {
        return $this->BelongsTo(RoleModel::class, 'role_id');
    }
}